﻿using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI.Utils;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class CarFollowPath : CarBaseNode
    {

        //public BBParameter<AnimationCurve> swerveSpeedCurve;

        public BBParameter<float> slowDownAngle = 10;
        public BBParameter<float> slowDownMinVelocity = 3;
        public BBParameter<float> handbrakeMinVelocity = 6;

        protected override void OnExecute()
        {
            if ((_data.carStatus & CarStatus.PathControl) == 0 && 
                (_data.carStatus & CarStatus.SpecialPathControl) == 0)
            {
                EndAction(false);
                return;
            }

            _carEntity.vehicleAI.Active = true;
            _carEntity.vehicleAI.SwitchSpeedStrategy(VehicleAI.VehicleSpeedStrategy.Const | VehicleAI.VehicleSpeedStrategy.HandBrake);

            PathPointData point =
                LibertyAIUtils.TrafficManager.PathManager.GetPathPoint(_data.pathId, _data.pathPointIndex);
            if (point != null)
            {
                _carEntity.SetDestination(point.position,point == LibertyAIUtils.TrafficManager.PathManager.GetTerminalPoint(_data.pathId));
            }
        }

        protected override void OnUpdate()
        {
            Vector3 frontPos = _carEntity.vehicleSetting.FrontPosition;
            Vector3 carForward = _carEntity.forward;
            float dis = Vector3.Distance(frontPos, _data.destination);

            PathPointData currentPoint =
                AIGraphUtils.TrafficManager.PathManager.GetPathPoint(_data.pathId, _data.pathPointIndex);

            // bool terminal = _data.terminal;
            // if (terminal)
            // {
            //     
            //     return;
            // }
            
            PathPointData nextPoint =
                AIGraphUtils.GetNextPoint(_data.pathId, _data.pathPointIndex,PathDirection.Forward);
            
            float dAngle = Vector3.Angle(_data.destination - frontPos, _carEntity.vehicleSetting.frontPoint.forward);
            
            //------- 跟随路径时的转弯减速逻辑
            

            float pathAngle = Vector3.Angle(nextPoint.position - currentPoint.position, carForward);
            float carVelocity = 0;
            if (_carEntity.vehicleAI.AIMode == VehicleAI.VehicleAIMode.Curve)
            {
                carVelocity = _carEntity.vehicleAI.curveModeData.followSlowDownFactor * 
                              _carEntity.vehicleAI.curveModeData.velocity.magnitude;
            }
            else
            {
                carVelocity = _carEntity.carController.getForwardVelocity();
            }
            
            _carEntity.carController.applyHandbrake = false;
            _carEntity.vehicleAI.externalMotorFactor = 1f;
            _carEntity.vehicleAI.splineModeData.applyHandbrake = false;
            _carEntity.vehicleAI.splineModeData.accFactor = 1f;
            _carEntity.vehicleAI.curveModeData.applyHandbrake = false;
            _carEntity.vehicleAI.curveModeData.accFactor = 1f;
            if (pathAngle > slowDownAngle.value || dAngle > 25)
            {
                if (carVelocity > slowDownMinVelocity.value)
                {
                    // 停止加速
                    _carEntity.vehicleAI.externalMotorFactor = 0f;
                    _carEntity.vehicleAI.splineModeData.accFactor = 0f;
                    _carEntity.vehicleAI.curveModeData.accFactor = 0f;
                    if (carVelocity > handbrakeMinVelocity.value)
                    {
                        // 点手刹
                        _carEntity.carController.applyHandbrake = true;
                        _carEntity.vehicleAI.splineModeData.applyHandbrake = true;
                        _carEntity.vehicleAI.curveModeData.applyHandbrake = true;
                    }
                }
                else
                {
                    // 走曲线因子
                }
            }

            //------

            float minDis = _carEntity.vehicleAI.AIMode == VehicleAI.VehicleAIMode.Curve ? 1.5f : 5f;
            
            if (dAngle >= 90)
            {
                int c = 0;
                PathPointData pointData = AIGraphUtils.GetNextPoint(_data.pathId, _data.pathPointIndex,PathDirection.Forward);
                while (c <= 4 && 
                       Vector3.Angle(pointData.position - frontPos, _carEntity.vehicleSetting.frontPoint.forward) >= 90 &&
                       Vector3.Distance(pointData.position,frontPos) < 35)
                {
                    pointData = AIGraphUtils.GetNextPoint(pointData.pathId, pointData.pointIndex,PathDirection.Forward);
                    c++;
                }
                
                if (pointData == null)
                {
                    EndAction(false);
                    return;
                }
                
                _data.pathId = pointData.pathId;
                _data.pathPointIndex = pointData.pointIndex;
                
                _carEntity.SetDestination(pointData.position,
                    pointData == LibertyAIUtils.TrafficManager.PathManager.GetTerminalPoint(_data.pathId));
            }
            else if (dis < minDis)
            {
                // 到达目标点，切换到下一个目标点
                
                PathPointData pointData =
                    AIGraphUtils.GetNextPoint(_data.pathId, _data.pathPointIndex,PathDirection.Forward);

                if (pointData == null)
                {
                    EndAction(false);
                    return;
                }
                
                _data.pathId = pointData.pathId;
                _data.pathPointIndex = pointData.pointIndex;
                
                _carEntity.SetDestination(pointData.position,
                    pointData == LibertyAIUtils.TrafficManager.PathManager.GetTerminalPoint(_data.pathId));
            }
        }
    }
}